package h0803;

import java.util.Arrays;
/**
  * @description 最大时间
  * @author 不知名帅哥
  * @date 2024/8/3 10:44
  * @version 1.0
*/
public class MaxTimeFinder {
    private static String maxTime = "invalid"; // Store the maximum valid time as a string

    public static void main(String[] args) {
        int[] digits = {5, 4, 3, 0, 5, 6};
        permute(digits, 0);
        System.out.println(maxTime);
    }

    // Function to generate permutations and evaluate them
    //求全排列
    private static void permute(int[] arr, int k) {
        if (k == arr.length) {
            //判断是否合法
            evaluateAndSetMaxTime(arr);
        } else {
            for (int i = k; i < arr.length; i++) {
                swap(arr, i, k);
                permute(arr, k + 1);
                swap(arr, k, i); // Backtrack to the original configuration
            }
        }
    }

    // Swap elements at positions i and j in array arr
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // Evaluate a given permutation of digits and update maxTime if this permutation is valid and greater
    private static void evaluateAndSetMaxTime(int[] perm) {
        int hh = perm[0] * 10 + perm[1];
        int mm = perm[2] * 10 + perm[3];
        int ss = perm[4] * 10 + perm[5];

        if (hh < 24 && mm < 60 && ss < 60) {
            String currentTime = String.format("%02d:%02d:%02d", hh, mm, ss);
            if (maxTime.equals("invalid") || currentTime.compareTo(maxTime) > 0) {
                maxTime = currentTime;
            }
        }
    }
}
